﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using Schuifpuzzel.Model;
using Microsoft.VisualBasic;
using Schuifpuzzel.MyKinect;

namespace Schuifpuzzel
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>

    public partial class MainWindow
    {
        private enum Screen
        {
            HighScore,
            Puzzle
        }
        private HighScoresScreen _highScoresScreen;
        private SlidePuzzleScreen _puzzleScreen;
        private HighScoreList _highScoreList;
        private Screen _currentScreen;
        public MainWindow()
        {
            InitializeComponent();

            this.ResizeMode = ResizeMode.NoResize;
            this.WindowStyle = WindowStyle.None;
            this.WindowState = WindowState.Maximized;

            _puzzleScreen = new SlidePuzzleScreen();
            _puzzleScreen.SetSize(SystemParameters.PrimaryScreenWidth, SystemParameters.PrimaryScreenHeight);
            _puzzleScreen.PuzzleEnd += PuzzleEnd;

            _highScoresScreen = new HighScoresScreen();            
            _highScoresScreen.RequestStartPuzzle += RequestStartPuzzle;

            //Try to get the stored high scores

            //If no list was returned, create a default list

                _highScoreList = new HighScoreList();
                Score score1 = new Score(250, 230, "Jos", null);
                Score score2 = new Score(300, 250, "Jef", null);
                Score score3 = new Score(300, 210, "Pieter", null);
                Score score4 = new Score(450, 280, "Bert", null);
                Score score5 = new Score(470, 200, "Tim", null);

                _highScoreList.AddScore(score1);
                _highScoreList.AddScore(score2);
                _highScoreList.AddScore(score3);
                _highScoreList.AddScore(score4);
                _highScoreList.AddScore(score5);

            _highScoresScreen.HighScoreList = _highScoreList;
            _currentScreen = Screen.HighScore;
            contentControl.Content = _highScoresScreen;
            _highScoresScreen.Start();
           // RequestStartPuzzle();
        }

        void PuzzleEnd()
        {
            this.Focus();
            _highScoreList = _puzzleScreen.HighScoreList;
            _highScoresScreen.HighScoreList = _highScoreList;
            _currentScreen = Screen.HighScore;
            contentControl.Content = _highScoresScreen;
            _highScoresScreen.Start();
        }

        void RequestStartPuzzle()
        {
            this.Focus();
            _currentScreen = Screen.Puzzle;
            _puzzleScreen.HighScoreList = _highScoreList;
            _puzzleScreen.Start();
            contentControl.Content = _puzzleScreen;
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                if (_currentScreen == Screen.Puzzle)
                {
                    MsgBoxResult msgBoxResult =
                        Interaction.MsgBox("Are you sure you want to go back to the High Scores?", MsgBoxStyle.YesNo,
                                           "Close application");
                    this.Focus();
                    if (msgBoxResult == MsgBoxResult.Yes)
                    {
                        _puzzleScreen.Stop();
                    }
                }
                else
                {
                    MsgBoxResult msgBoxResult = Interaction.MsgBox("Are you sure you want to close the application?", MsgBoxStyle.YesNo, "Close application");
                    this.Focus();

                    if (msgBoxResult == MsgBoxResult.Yes)
                    {
                        this.Close();
                    }
                }               
            }

        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if(KinectForPuzzle.GetInstance().Kinect!= null)
            {
                KinectForPuzzle.GetInstance().Kinect = null;
            }
        }


    }
}
